//
//  GYMineBuyInfoVC.m
//  GYDemo
//
//  Created by mac on 2018/3/27.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYMineBuyInfoVC.h"
#import "Head.h"
#import "GYChooseView.h"

@interface GYMineBuyInfoVC ()
{
    NSInteger currentPage;
    NSString *name;
    NSString *protypeid;
}
@property (nonatomic,strong) GYMineBuyInfoViewModel *buyInfoviewModel;
@property (nonatomic,strong) GYChooseView *flitHeader;
@end

@implementation GYMineBuyInfoVC
#pragma mark - getter and setter
-(GYMineBuyInfoViewModel *)buyInfoviewModel
{
    if (!_buyInfoviewModel) {
        _buyInfoviewModel = [GYMineBuyInfoViewModel new];
    }
    return _buyInfoviewModel;
}
-(UIView *)flitHeader
{
    if (!_flitHeader) {
        _flitHeader = [[GYChooseView alloc]initWithFrame:WFCGRectMake(0, 0, 375, 45) chooseViewType:ChooseViewTypeSingleCenter];
        _flitHeader.backgroundColor = Main_Color_White;
        @WeakSelf;
        [_flitHeader setCategoryback:^(NSString *categoryStr) {
            
        }];
        [_flitHeader setChooseBack:^(NSDictionary *paraDic) {
            currentPage = 1;
            name = paraDic[@"keyword"] ?: @"";
            protypeid = paraDic[@"protypeid"] ?: @"";
            [selfp reloadData];
        }];
    }
    return _flitHeader;
}
#pragma mark - event
#pragma mark - method
-(void)initilization
{
    self.navigationItem.title = @"我的求购信息";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.navigationController.navigationBar.shadowImage = [[UIImage new]rt_darkenWithLevel:0.3f];
    [self setNavgationTheme:1];
    [self.view addSubview:self.flitHeader];
    [self.view addSubview:self.baseTable];
    [self.baseTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.baseTable setSeparatorColor: Main_Color_BG];
    [self.baseTable setBackgroundColor:Main_Color_BG];
    [self.baseTable setTintColor:Main_Color_BG];
    self.baseTable.y = self.flitHeader.bottom;
    self.baseTable.height = WF_UI_VIEW_HEIGHT - self.bottomShelterHeight - self.flitHeader.height;
    if (@available(iOS 11.0, *)) {
        //        self.baseTable.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
    }
    self.baseTable.mj_header.backgroundColor = Main_Color_Red;
    self.baseTable.scrollIndicatorInsets = self.baseTable.contentInset;
    [self viewModelBand];
    [self judgeValue];
    
    @WeakSelf;
    self.baseTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        currentPage = 1;
        name = nil;
        protypeid = nil;
        [selfp reloadData];
    }];
    self.baseTable.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        currentPage += 1;
        [selfp reloadData];
    }];
    
    [self reloadData];
}
-(void)viewModelBand
{
    @WeakSelf;
    GCBaseObservationModel *observation = [GCBaseObservationModel new];
    observation.observation = self.buyInfoviewModel;
    observation.keyPath = @"datas";
    observation.handler = ^(NSString *keyPath) {
        [selfp reloadView];
    };
    [self registObservation:observation];
}
-(void)reloadView
{
    [self.baseTable reloadData];
}
-(void)reloadData
{
    NSMutableDictionary *para = [NSMutableDictionary new];
    NSString *page = [NSString stringWithFormat:@"%ld",currentPage];
    NSString *pageSize = @"10";
    [para setObject:page forKey:@"page"];
    [para setObject:pageSize forKey:@"pagesize"];
    !name ?: [para setObject:name forKey:@"name"];
    !protypeid ?: [para setObject:protypeid forKey:@"protypeid"];
    [self.buyInfoviewModel requestDatas:para];
}
-(void)refreshData
{
    currentPage = 1;
    [self reloadData];
}
- (void)judgeValue {
    
    @WeakSelf;
    [self setRightBarButtonWithTitle:@"新增求购" titleColor:Main_Color_Red withBlock:^() {
        GYMineBuyInfoEditVC *edit = [GYMineBuyInfoEditVC new];
        edit.listVC = selfp;
        [selfp.navigationController pushViewController:edit animated:YES];
    }];
}

#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initilization];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.buyInfoviewModel.datas.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellID = @"CellID";
    GYMineBuyInfoModel *model = self.buyInfoviewModel.datas[indexPath.row];
    GYMineBuyInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil) {
        cell = [[GYMineBuyInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
    }
    @WeakSelf
    [cell.upBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        [selfp.buyInfoviewModel requestUpIndex:indexPath.row];
    }];
    [cell.downBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        [selfp.buyInfoviewModel requestDownIndex:indexPath.row];
    }];
    [cell.deleteBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        [selfp tableView:tableView commitEditingStyle:UITableViewCellEditingStyleDelete forRowAtIndexPath:indexPath];
    }];
    cell.model = model;
    return cell;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GYMineBuyInfoModel *model = self.buyInfoviewModel.datas[indexPath.row];
    if (1 == model.down) {
        return UITableViewCellEditingStyleNone;
    }else{
        return UITableViewCellEditingStyleDelete;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = [GYMineBuyInfoCell finalHeight];
    return height;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (UITableViewCellEditingStyleDelete == editingStyle) {
        //删除
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定删除吗？" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"删除", nil];
        @WeakSelf
        [alert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
            if (1 == buttonIndex) {
              //  GYMineBuyInfoModel *model = selfp.buyInfoviewModel.datas[indexPath.row];
//                [selfp.buyInfoviewModel requestDelete:model];
                [selfp.buyInfoviewModel requestDeleteIndex:indexPath.row];
            }
        }];
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GYMineBuyInfoModel *model = self.buyInfoviewModel.datas[indexPath.row];
    GYMineBuyInfoDetailVC *detail = [GYMineBuyInfoDetailVC new];
    detail.listVC = self;
    detail.buyInfoModel = model;
    [self.navigationController pushViewController:detail animated:YES];
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
